package 尚硅谷juc大厂.test8;


import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class LockSupportDemo {

    static Lock lock=new ReentrantLock();
    static Condition condition=lock.newCondition();

    /**
     *Condition的await/signal必须配合ReentrantLock才能使用
     *我们将lock和unlock注释掉，运行程序会抛出IllegalMonitorStateException异常
     */
    public static void main(String[] args) {
        new Thread(()->{
            //lock.lock();
            try {
                System.out.println(Thread.currentThread().getName()+"线程执行......");
                condition.await();
                System.out.println(Thread.currentThread().getName()+"被唤醒了......");
            }catch (Exception exception){
                exception.printStackTrace();
            }finally {
               // lock.unlock();
            }
        },"AA").start();

        new Thread(()->{
            //lock.lock();
            try {
                condition.signal();
                System.out.println(Thread.currentThread().getName()+"执行了通知......");
            }catch (Exception exception){
                exception.printStackTrace();
            }finally {
            //    lock.unlock();
            }
        },"BB").start();
    }
}
